﻿Imports MySql.Data.MySqlClient
Public Class frmeditbooking_voyage
    Dim mysql As MySqlConnection = main_form.mysqlconection
    Dim mySqlCommand As New MySqlCommand
    Dim mySqlAdaptor As New MySqlDataAdapter
    Dim mySqlReader As MySqlDataReader
    Dim respone As Object
    Dim idvoymove As Integer

    Private Sub frmeditbooking_voyage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
        System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")

        txt_booking.Text = frmview_voyage.idbooking
        mysql.Close()

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If

        mySqlCommand.CommandText = "Select * from voyage  order by VOYAGEID DESC  LIMIT 25;"
        ' mySqlCommand.CommandText -0mySqlCommand.Connection = mysql

        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand
        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())

                With ListView1.Items.Add(Format(mySqlReader("VOYAGEID"), "000"))
                    .SubItems.Add("N")
                    .SubItems.Add(mySqlReader("VOYVESNAMEN"))
                    .SubItems.Add(mySqlReader("VOYDATESN"))
                    .SubItems.Add(mySqlReader("VOYDATEEN"))
                    .SubItems.Add(Format(mySqlReader("VOYAGEID"), "000"))
                    .SubItems.Add("S")
                    .SubItems.Add(mySqlReader("VOYVESNAMES"))
                    .SubItems.Add(mySqlReader("VOYDATESS"))
                    .SubItems.Add(mySqlReader("VOYDATEES"))
                End With

            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()

    End Sub

    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        idvoymove = ListView1.SelectedItems(0).SubItems(0).Text
    End Sub
    Public Sub searchVoyage()
        mysql.Close()

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If

        mySqlCommand.CommandText = "Select * from voyage where VOYAGEID like '%" & TextBoxX1.Text & "%';"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader
            ListView1.Items.Clear()
            While (mySqlReader.Read())


                With ListView1.Items.Add(Format(mySqlReader("VOYAGEID"), "000"))
                    .SubItems.Add("N")
                    .SubItems.Add(mySqlReader("VOYVESNAMEN"))
                    .SubItems.Add(mySqlReader("VOYDATESN"))
                    .SubItems.Add(mySqlReader("VOYDATEEN"))
                    .SubItems.Add(Format(mySqlReader("VOYAGEID"), "000"))
                    .SubItems.Add("S")
                    .SubItems.Add(mySqlReader("VOYVESNAMES"))
                    .SubItems.Add(mySqlReader("VOYDATESS"))
                    .SubItems.Add(mySqlReader("VOYDATEES"))
                End With
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()
    End Sub

    Private Sub ButtonX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX3.Click
        searchVoyage()

    End Sub

    Private Sub TextBoxX1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxX1.KeyDown
        If e.KeyCode = Keys.Enter Then
            searchVoyage()
        End If
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click

        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        Try
            Dim commandText2 As String
         
            commandText2 = "UPDATE booking  left join borrow  on booking.BOOKINGID  = borrow.BOOKID left   join ctnmain on borrow.CTNID = ctnmain.CTNMAINID  SET BVOYAGE ='" & idvoymove & "' , CTNVOYS = '" & idvoymove & "' WHERE BOOKINGID = " & txt_booking.Text & "; "
            mySqlCommand.CommandText = commandText2
            mySqlCommand.CommandType = CommandType.Text
            mySqlCommand.Connection = mysql
            mySqlCommand.ExecuteNonQuery()
            mysql.Close()

            MsgBox("แก้ไขข้อเรียบร้อย")
            Dim cf As New frmview_voyage

            cf.MdiParent = Me.MdiParent
            Me.Close()
            cf.Dock = DockStyle.Fill
            cf.Show()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class